﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.pro2e.console.service.auth;
using com.pro2e.console.service.auth.vo;
using NHibernate;
using com.pro2e.web;
using com.pro2e.web.log;
using com.pro2e.web.util;
using com.pro2e.web.util.page;
using com.pro2e.console.service.msg.vo;

public partial class adm_Login_Login : System.Web.UI.Page
{
    log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    AuthService authService = new AuthService();
    WebLogService webLogService = new WebLogService();

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    private void doLogin(string id, string pw)
    {
        //帳號皆改為小寫
        if (!string.IsNullOrEmpty(id))
        {
            id = id.ToLower();
        }

        LoginUser loginUser = authService.Login(id, pw);

        if (loginUser != null)
        {
            SessionHelper sHelper = new SessionHelper();

            sHelper.LoginUser = loginUser;

            //加入log
            webLogService.AddSystemLogLogin(loginUser.UserId);

            NHibernateUtil.Initialize(loginUser.BelongRoles);

            //清除快取
            UserMenuFuncContainer.GetInstance().ReloadAllMenu();

            HttpHelper httpHelper = new HttpHelper();
            //string referer = httpHelper.GetReferer(HttpContext.Current);

            Response.Redirect("~/admin/index.aspx", false);

            return;
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(lblMsg, lblMsg.GetType(), "js", JavascriptUtil.AlertJS(MsgVO.LOGIN_ERROR), false);
        }
    }

 

    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        string id = txtId.Text.Trim();
        string pw = txtPw.Text.Trim();
        doLogin(id, pw);
    }
    protected void btnClear_Click(object sender, ImageClickEventArgs e)
    {
        txtId.Text = string.Empty;
        txtPw.Text = string.Empty;
        txtId.Focus();
    }
}
